package com.example.hyznbk.view.base;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hyznbk.R;
import com.example.hyznbk.view.widget.EmptyMainLayout;
import com.lancoo.reviewsystem.utils.DensityUtils;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.reflect.Field;

/**
 * Created by tangminglong on 17/7/26.
 */

public abstract class BaseActivity extends RxAppCompatActivity implements IBaseView,EmptyMainLayout.OnRetryListener {


    @Nullable
    protected EmptyMainLayout mEmptyViewLayout;

    @Nullable
    protected RelativeLayout mLayoutToolBar;

    @Nullable
    protected RelativeLayout mLayoutNote;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置只能竖屏
        //适配7.0手机 状态栏蒙了一层灰色的问题
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);  //改为透明
            } catch (Exception e) {}
        }
        setContentView(attachLayoutRes());
        mLayoutToolBar = (RelativeLayout) findViewById(R.id.topbar_activity);
        mLayoutNote = (RelativeLayout) findViewById(R.id.rlayout_knowledge_bar);
        if (mLayoutToolBar != null){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mLayoutToolBar.getLayoutParams();
            layoutParams.setMargins(0,getStatusBarHeight(),0,0);
        }
        //mEmptyViewLayout = findViewById(R.la)
        initViews();
        updateViews(true);
    }

    /**
     * 绑定视图
     *
     * @return
     */
    @LayoutRes
    public abstract int attachLayoutRes();

    /**
     * 初始化视图控件
     */
    protected abstract void initViews();

    /**
     * 更新新视图控件
     *
     * @param isRefresh 是否是刷新
     */
    protected abstract void updateViews(boolean isRefresh);

    @Override
    public RxAppCompatActivity getHolderActivity() {
        return this;
    }

    @Override
    public void showLoading() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.hide();
        }
    }

    @Override
    public void hideLoading(final HideLoadingFinished hideLoadingFinished) {
       hideLoading();

        mEmptyViewLayout.getGifView().post(new Runnable() {
            @Override
            public void run() {
                hideLoadingFinished.hideFinished();
            }
        });
       /* mEmptyViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                hideLoadingFinished.hideFinished();
                mEmptyViewLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });*/
    }

    @Override
    public void showNetError() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_NO_NET);
            mEmptyViewLayout.setRetryListener(this);
        }
    }

    @Override
    public void showNoData(String msg) {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_NO_DATA);
            mEmptyViewLayout.setEmptyMessage(msg);
        }
    }


    @Override
    public void onRetry() {
        updateViews(true);
    }

    /**
     * 初始化 Toolbar
     *
     * @param toolbar
     * @param homeAsUpEnabled
     * @param title
     */
    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title) {
        toolbar.setTitle("");
        ((TextView) toolbar.findViewById(R.id.tv_toolbar_title)).setText(title);
        setSupportActionBar(toolbar);
        toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle) {
        initToolBar(toolbar, homeAsUpEnabled, getString(resTitle));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 获取系统状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return 0;
        } else {
            int statusBarHeight = DensityUtils.dp2px(this, 25);
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
                statusBarHeight = getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return statusBarHeight;
        }

    }

    public int getToolBarHeight(){
        if (mLayoutToolBar != null){
            return mLayoutToolBar.getHeight();
        }else {
            return 0;
        }

    }

    public int getNoteHeigt(){
        if (mLayoutNote != null && mLayoutNote.getVisibility() == View.VISIBLE){
            return mLayoutNote.getHeight();
        }else {
            return 0;
        }
    }

}
